//grid 导出功能支持
/**
 * grid导出函数
 * grid_id:gridid
 * export_id 表单id
 * searchbox_id:查询盒子ID
 */
function grid_export(grid_id,export_id,searchbox_id){
	var ids = $('#'+grid_id).datagrid('getSelections');
	if(ids.length==0){	
		$.messager.confirm('提示','是否要导出所有数据？',function(r){ 			
		    if (r){   	
		    	var meta_str=Serialize(getQueryArgsInfo(grid_id));
				var filter_str=getSearchBoxFilter(searchbox_id);			
		    	var args_filter=get_export_attr('filter',export_id);
		    	var args_meta=get_export_attr('meta',export_id);
		    	 if(null==args_filter){
			        	$("#"+export_id).append("<input type=\"hidden\" name=\"filter\" value=\""+encodeURI(filter_str)+"\" />");
			        }
		    	 else{
		    		 set_export_attr('filter',encodeURI(filter_str),export_id);
		    	 }
		    	 if(null==args_meta){
			        	$("#"+export_id).append("<input type=\"hidden\" name=\"meta\" value=\""+encodeURI(meta_str)+"\" />");
			        }
		    	 else{
		    		 set_export_attr('meta',encodeURI(meta_str),export_id);
		    	 }
		    	 set_export_attr('title',encodeURI(getTitle(grid_id)),export_id);
		    	$("#"+export_id).submit();
		    }   
		}); 
	}else{
		$.messager.confirm('提示','是否要导出当前选择的数据？',function(r){   
		    if (r){   
		        var args_ids=get_export_attr('ids',export_id);
		        var choosefield=get_export_attr('choosefield',export_id);//choosefield
		        var ids_str=getids(ids,choosefield);
		        if(null==args_ids){
		        	$("#"+export_id).append("<input type=\"hidden\" name=\"ids\" value=\""+ids_str+"\" />");
		        }
		        set_export_attr('title',encodeURI(getTitle(grid_id)),export_id);
		        $("#"+export_id).submit();
		    }   
		});
	}
	 //$("#export_id").submit();
}

function set_export_attr(attr_name,attr_value,export_id){
	$("input[type=hidden]",document.forms[export_id]).each(function(){
		  if(this.name==attr_name){
			  this.value=attr_value;
			 // break;
		  }
		 });
}


function get_export_attr(attr_name,export_id){
	var obj=null;
	$("input[type=hidden]",document.forms[export_id]).each(function(){
		  if(this.name==attr_name){
			  obj=this.value;
			 // break;
		  }
		 });
	return obj;
}
function getids(ids,field){
	var choose_str="";
	for(var i=0;i<ids.length;i++){
		 if(i==ids.length-1){
			 choose_str+="'"+eval("ids[i]."+field)+"'";
		 }else{
			 choose_str+="'"+eval("ids[i]."+field)+"',";
		 }
	 }
	return choose_str;
}

/**
 * 得到所有列的标题 
 * @param grid_id
 * @return
 */
function getTitle(grid_id){
	var sss=$('#'+grid_id).datagrid('options').columns;
	var frozencloums=$('#'+grid_id).datagrid('options').frozenColumns;
	var titlestr="";
	for(var i=0;i<frozencloums.length;i++){
		for(var j=0;j<frozencloums[i].length;j++){
			//列为checkbox类型的标题不用导出
			if("undefined" != typeof frozencloums[i][j].checkbox && frozencloums[i][j].checkbox==true){
				continue;
			}else if("undefined" == typeof frozencloums[i][j].title ||frozencloums[i][j].title==""){
				continue;
			}else{
				titlestr+=frozencloums[i][j].title+",";				
			}
		}
	}	
	for(var i=0; i<sss.length; i++){
		for(var j=0;j<sss[i].length;j++){
			//列为checkbox类型的标题不用导出
			if("undefined" != typeof sss[i][j].checkbox && sss[i][j].checkbox==true){
				continue;
			}else if("undefined" == typeof sss[i][j].title ||sss[i][j].title==""){
				continue;
			}else{
				titlestr+=sss[i][j].title+",";				
			}
		}
				
	}
	
		
	return titlestr;
}